import { save, list } from "../../api/product";
import { isNull } from "../../utils/util";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        data: {
            price: 0,
            minimum: 1,
            limitDay: 30,
            goodsId: null,
            name: '',
            customerId: null,
            sn: '',
        },
        sellerName: '',
        linkman:''
    },

    bindPrice(e) {
        let price = 0;
        if (e.detail.value > 10000000) {
            price = 10000000;
        } else if (e.detail.value < 0) {
            price = 0
        } else {
            price = e.detail.value;
        }
        this.setData({
            [`data.price`]: price
        })
    },

    bindMinimum(e) {
        let minimum;
        if (e.detail.value >= 1000000) {
            minimum = 1000000;
            wx.showToast({
                title: '不能大于1000000吨',
                icon: 'none'
            })
        } else if (e.detail.value < 1) {
            minimum = 1
            wx.showToast({
                title: '不能小于1吨',
                icon: 'none'
            })
        } else {
            minimum = e.detail.value*1;
        }
        this.setData({
            [`data.minimum`]: minimum
        })
    },

    bindDay(e) {
        let day = 0;
        if (e.detail.value >= 1000000) {
            day = 1000000;
        } else if (e.detail.value < 0) {
            day = 0
        } else {
            day = e.detail.value;
        }
        this.setData({
            [`data.limitDay`]: day
        })
    },

    selectGoods() {
        wx.navigateTo({
            url: '/pages/product/selectGoods',
        })
    },
    add() {
        let _this = this.data;
        if (isNull(_this.data.goodsId)) {
            wx.showToast({
                icon: 'none',
                title: '请选择商品',
            })
        } else if (isNull(_this.data.customerId)) {
            wx.showToast({
                icon: 'none',
                title: '请选择卖家',
            })
        } else if (_this.data.price <= 0) {
            wx.showToast({
                icon: 'none',
                title: '请输入正确价格',
            })
        } else if (isNull(_this.data.limitDay)) {
            wx.showToast({
                icon: 'none',
                title: '请输入锁油期限',
            })
        } else if (isNull(_this.data.minimum)) {
            wx.showToast({
                icon: 'none',
                title: '请输入起订量',
            })
        } else {
            save(this.data.data).then(res => {
                wx.navigateBack()
            })
        }
    },
    selectSeller() {
        wx.navigateTo({
            url: '/pages/product/seller',
        })
    },
    lowerNum() {
        let lower = this.data.data.minimum;
        if (lower <= 1) {
            this.setData({
                [`data.minimum`]: 1
            })
            wx.showToast({
                title: '不能小于1吨',
                icon: 'none'
            })
        } else {
            this.setData({
                [`data.minimum`]: lower - 0.5
            })
        }
    },

    addNum() {
        let lower = this.data.data.minimum;
        if (lower >= 1000000) {
            this.setData({
                [`data.minimum`]: 1000000
            })
            wx.showToast({
                title: '不能大于1000000吨',
                icon: 'none'
            })
        } else {
            this.setData({
                [`data.minimum`]: lower + 0.5
            })
        }
    },
    onShow() {
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页
        if (currPage.data.memberId != undefined || currPage.data.goodsId != undefined) {
            console.log(currPage.data);
            //调取传进的参数
            this.setData({
                [`data.goodsId`]: currPage.data.goodsId,
                [`data.name`]: currPage.data.goodsName,
                [`data.sn`]: currPage.data.goodsSn,
                [`data.customerId`]: currPage.data.memberId,
                sellerName: currPage.data.sellerName
            })
        }
    }
})